home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tree / tpar.l < prev    next >
Text File  |  1993-09-01  |  4KB  |  207 lines

  1. %{
  2. /*
  3.  * tpar: pre-processor for tree; adds parens according to indentation.
  4.  * Version 0.1
  5.  * 
  6.  * Blank line ends the tree.
  7.  * ` - ' adds following item on same line as single child.
  8.  * Does not do forests -- add outer parens in source as part of
  9.  *     beginning and ending node names or use null command \Z.
  10.  * Option -t converts N',V',A',P' by adding TeX \overline commands,
  11.  *     and [<digit or i,j,k,x,y,z>] by adding TeX subscripting
  12.  *     commands.
  13.  * Option -v puts right parentheses on new lines, with indentation
  14.  *     the same as the preceding matching paren.
  15.  * 
  16.  *             Greg, lee@uhccux.uhcc.hawaii.edu, 6/24/90
  17.  */
  18.  
  19. #define TRUE 1
  20. #define FALSE 0
  21. int tex_opt = FALSE;
  22. int v_opt = FALSE;
  23.  
  24. int outercol = 0;
  25. int linenumber = 0;
  26. int indent = 0;
  27. int minindent = 0;
  28. int level = 0;
  29. int buried[100];
  30. int extra = 0;
  31. %}
  32.  
  33. %s T O X
  34.  
  35. %%
  36.  
  37. <O>\n {
  38.     outercol = 0;
  39.     indent = 0;
  40.     linenumber++;
  41.     ECHO;
  42. }
  43.  
  44. <T,X>\n {
  45.     if (!outercol) {
  46.         descend(minindent);
  47.         ECHO;
  48.         BEGIN(O);
  49.     }
  50.     indent = outercol = 0;
  51.     linenumber++;
  52. }
  53.  
  54. <O>\t {
  55.     outercol++;
  56.     while (outercol % 8) outercol++;
  57.     ECHO;
  58. }
  59.  
  60. <T,X>\t {
  61.     outercol++;
  62.     while (outercol % 8) outercol++;
  63. }
  64.  
  65. <T,X>" " {
  66.     outercol++;
  67.     if (indent) ECHO;
  68. }
  69.  
  70. <T,X>[ \t]+"-"[ \t]+ {
  71.     extra++;
  72.     printf("(");
  73. }
  74.  
  75. \\tree[ \t]*(-([tuvLTOIFER]+|[bg][0-9]+)[ \t]*)* {
  76.     outercol += yyleng;
  77.     level = extra = 0;
  78.     minindent = 200;
  79.     buried[0] = -1;
  80.     if (tex_opt) BEGIN(X);
  81.     else BEGIN(T);
  82.     ECHO;
  83. }
  84.  
  85. <O>. {
  86.     outercol++;
  87.     ECHO;
  88. }
  89.  
  90. <X>[NVAP]' {
  91.     ascend();
  92.     outercol += 2;
  93.     printf("$\\overline{\\rm %c}$", yytext[0]);
  94. }
  95.  
  96. <X>\[[0-9ijkxyz]\] {
  97.     ascend();
  98.     outercol += 3;
  99.     printf("$_%c$", yytext[1]);
  100. }
  101.  
  102. <T,X>\\Z[ \t]*$ {
  103.     ascend();
  104.     outercol += 2;
  105. }
  106.  
  107. <T,X>. {
  108.     ascend();
  109.     outercol++;
  110.     ECHO;
  111. }
  112.  
  113. %%
  114.  
  115. descend(indent)
  116. int indent;
  117. {    int i;
  118.  
  119.     if (extra) do printf(")"); while (--extra);
  120.     if (indent > buried[level]) {
  121.         buried[++level] = indent;
  122.     }
  123.     else {
  124.         printf(")");
  125.         while (level >= 0 && indent < buried[level]) {
  126.             level--;
  127.             if (v_opt) {
  128.                 printf("\n");
  129.                 for (i = 1; i < buried[level]; i++)
  130.                     printf(" ");
  131.             }
  132.             printf(")");
  133.         }
  134.     }
  135.     printf("\n");
  136. }
  137.  
  138. ascend()
  139. {    int i;
  140.  
  141.     if (indent) return;
  142.     descend(indent = outercol+1);
  143.     if (indent < minindent) minindent = indent;
  144.     for (i = 1; i < indent; i++) printf(" ");
  145.     printf("(");
  146. }
  147.  
  148. extern char *optarg;        /* from getopt */
  149. extern int  optind;
  150.  
  151. main(argc, argv)
  152. int     argc;
  153. char   *argv[];
  154. {    int c;
  155.     char *progname = NULL, *basename();
  156.  
  157.     progname = basename (argv[0]);
  158.     while ((c = getopt (argc, argv, "htv")) != EOF)
  159.         switch (c) {
  160.             case 't': tex_opt = TRUE; break;
  161.             case 'v': v_opt = TRUE; break;
  162.             case 'h':
  163.                 default: 
  164.            fprintf(stderr, "Usage: %s [options] [files]\n", progname);
  165.            fprintf(stderr, "options = -t\t(TeX code)\n");
  166.            fprintf(stderr, "          -v\t(parens on new lines)\n");
  167.            fprintf(stderr, "          -h\t(print this information)\n");
  168.            exit(1);
  169.         }
  170.  
  171.     BEGIN(O);
  172.  
  173.     if (optind >= argc) {
  174.         (void) yylex ();
  175.     }
  176.     else for (; (optind < argc); optind++) {
  177.         if (yyin == NULL) yyin = stdin;
  178.         if (freopen (argv[optind], "r", stdin) != NULL) {
  179. #ifdef FLEX_SCANNER    
  180.             /* to get flex to look at > 1 file */
  181.             yy_init = 1;
  182. #endif
  183.             (void) yylex ();
  184.                 if (level > 1) descend(minindent);
  185.             outercol = linenumber = indent = minindent = 0;
  186.             level = extra = 0;
  187.         }
  188.         else {
  189.             (void) fprintf (stderr,
  190.                 "Couldn't open file: %s\n", argv[optind]);
  191.             exit (1);
  192.         }
  193.     }
  194.     if (level > 1) descend(minindent);
  195. }
  196.  
  197.  
  198. char   *basename (s)
  199. char   *s;
  200. {
  201.     char   *p, *strrchr();
  202.  
  203.     if (p = strrchr(s, '/'))
  204.         return(++p);
  205.     else return(s);
  206. }
  207.